$(function () {
    $('#input-keywords').prop('placeholder','请输入要搜索的内容');
    var puFlag = false;
    var cuFlag = false;

    var startTime = new Date().getTime();
    $('#fwdw').hover(function () {
        $('.menu-list').stop(true).show();
        if (G.request['areaid'] && G.request['areaid'] == 0) {
            $('.pu-frame').css('display', 'block');
            $('#pu').addClass('active');
        } else if (G.request['areaid'] && G.request['areaid'] > 0) {
            $('.cu-frame').css('display', 'block');
            $('#cu').addClass('active');
        } else {
            $('#all').css({
                'background': '#fff',
                "color": '#2f6ea2'
            });
        }
    }, function () {
        $('.menu-list').stop(true).hide();
        if (G.request['areaid'] && G.request['areaid'] == 0) {
            $('.pu-frame').css('display', 'none');
            $('#pu').removeClass('active');
        } else if (G.request['areaid'] && G.request['areaid'] > 0) {
            $('.cu-frame').css('display', 'none');
            $('#cu').removeClass('active');
        } else {
            $('#all').css({
                'background': '#fff',
                "color": '#515151'
            });
        }
    });

    $('#pu').hover(function () {
        $('.pu-frame').css('display', 'block');
        $('#pu').addClass('active');
    }, function () {
        $('.pu-frame').css('display', 'none');
        $('#pu').removeClass('active');
    });

    $('#cu').hover(function () {
        $('.cu-frame').css('display', 'block');
        $('#cu').addClass('active');
    }, function () {
        $('.cu-frame').css('display', 'none');
        $('#cu').removeClass('active');
    });

    $('#all').hover(function () {
        $(this).css({
            'background': '#fff',
            "color": '#2f6ea2'
        });
        $('#cu').removeClass('active');
        $('#pu').removeClass('active');
        $('.cu-frame').css('display', 'none');
        $('.pu-frame').css('display', 'none');
    }, function () {
        $(this).css({
            'background': '#fff',
            "color": '#515151'
        });
    });


    $('#drown-all').click(function () {
        delete options.prefix_url;
        window.options.areaid = '';
        search();
    });

    $('.pu-frame').on('click', '.item', function (e) {
        e.stopPropagation();
        var $that = $(this);
        $('.pu-frame').find('.item').each(function () {
            $(this).removeClass('ms');
        });
        $that.addClass('ms');
        $('.mask').fadeOut(200);
        var $dom = $('.pu-frame').hide(100).find('.ms');
        $('#goventity').html($dom.html());
        $('#drown-content').html($dom.html());
        window.options.prefix_url = $dom.attr('key');
        window.options.areaid = 0;
        setTimeout(function () {
            if (puFlag || $that.parents('.right-modal').length != 0) {
                search();
            }
        }, 100);
    });

    $('.cu-frame').on('click', '.item', function () {
        var $that = $(this);
        delete options.prefix_url;
        $('.cu-frame').find('.item').each(function () {
            $(this).removeClass('ms');
        });
        $that.addClass('ms');
        var $dom = $('.cu-frame').hide(100).find('.ms');
        $('#drown-content').html($dom.html());
        window.options.areaid = $dom.attr('key');
        if (cuFlag || $that.parents('.right-modal').length != 0) {
            // $('#advance-search-go').trigger('click');
            search();
        }
    });

    setTimeout(function () {
        var temp = G.request['prefix_url'];
        var tempArea = G.request['areaid'];
        if (temp) {
            var $keyDom = $('.item[key=' + temp + ']');
            $('#goventity').html($keyDom.html());
            $keyDom.addClass('ms');
            $('#drown-content').html($keyDom.html());
        } else if (tempArea > 0) {
            var $keyDom = $('.cu-frame').find('.item[key=' + tempArea + ']');
            $keyDom.addClass('ms');
            $('#cu').addClass('selected');
            $('#drown-content').html($keyDom.html());
        } else if (temp === '') {
            var $keyDom = $('.pu-frame').find('.item[key]').eq(0);
            $keyDom.addClass('selected');
            $('#drown-content').html('全部省直单位');
        } else if (tempArea === '' && temp === undefined) {
            $('#drown-content').html('全部单位');
            // $('#all').addClass('selected');
        } else {
            $('#drown-content').html('全部单位');
        }
    });

    $('#left-part').bindOptionPart({
        varname: 'timeRange'
    });
    $('#sort-way').bindOptionPart({
        varname: 'order'
    });
    $('#sort-select').bindOptionPart({
        varname: 'order'
    });
    $('#position-select').bindOptionPart({
        varname: 'position'
    });

    $('.menu-list').bindOptionPart({
        varname: 'areaid'
    });

    $('#advance-search-button-show').mousedown(function () {
        window.advance = true;
        sessionStorage.setItem('isShow', '1');
        $('.advance-search').fadeIn();
        $('#left-part').fadeOut();
        return false;
    });
    $('#advance-search-button-fold').mousedown(function () {
        window.advance = false;
        sessionStorage.setItem('isShow', '0');
        $('.advance-search').fadeOut();
        $('#left-part').fadeIn();
        return false;
    });
    $('#advance-search-go').click(function () {
        var i;
        var toCheck = ['order', 'time_from', 'time_to'];
        var args = {
            keywords: $iptInclude.val(),
            keywords_not: $iptExclude.val(),
            advance: 'true',
            order: 'order' in window.options ? window.options.order : '0',
            position: 'position' in window.options ? window.options.position : 'title',
        };
        for (i = 0; i < toCheck.length; i++) {
            if (toCheck[i] in window.options) {
                args[toCheck[i]] = window.options[toCheck[i]];
            }
        }
        G.go(G.url('index', 'local', args));
    });

    var $timeFrom = $('#time-from').asTimeSelect({
        varname: 'time_from'
    });
    var $timeTo = $('#time-to').asTimeSelect({
        varname: 'time_to'
    });

    var $iptInclude = $('#input-kw-include').val(G.request['keywords'] || '');
    var $iptExclude = $('#input-kw-exclude').val(G.request['keywords_not' || '']);

    // if ("advance" in G.request && sessionStorage.getItem('isShow') == '1') {
    //     $('#advance-search-button-show').mousedown();
    // }
    (function (o) {
        var m = {};
        var i;
        for (i in o) {
            if (i != 'c' && i != 'a') {
                m[i] = o[i];
            }
        }
        if ("timeRange" in m) {
            m['time_to'] = moment().unix();
            switch (m['timeRange']) {
                case 'day':
                    m['time_from'] = m['time_to'] - 86400;
                    break;
                case 'week':
                    m['time_from'] = m['time_to'] - 86400 * 7;
                    break;
                case 'month':
                    m['time_from'] = m['time_to'] - 86400 * 31;
                    break;
                case 'year':
                    m['time_from'] = m['time_to'] - 86400 * 365;
                    break;
            }
            delete m['timeRange'];
        }
        // if (!("keywords" in m) || m.keywords.length < 1) {
        //     $('#list-body').empty();
        //     return;
        // }
        if (!m.keywords || !m.keywords.length) {
            m.keywords = ' ';
        }
        if (!m.order) {
            m.order = 0;
        }
        G.call('search.local', m, function (c, d) {
            $('#page-list').refreshPage({
                cur: G.request['page'] || 1,
                max: d.page_total > 500 ? 500 : d.page_total
            });
            $('#list-body').refreshList({
                list: d.list,
                total: d.total,
                time: new Date().getTime() - startTime
            }, 'local');
        }, function (c, m) {
            alert(m);
        });
    })(G.request);

    var search = function (normalSearch) {
        var kw = G.request['keywords'];
        if (!kw || !kw.length) {
            kw = '';
            // return;
        }
        var data = {
            keywords: kw,
            order: 'order' in window.options ? window.options.order : '0',
        };
        if (window.options.areaid >= 0) {
            data.areaid = window.options.areaid;
        } else {
            data.areaid = '';
        }
        if (window.options.prefix_url !== undefined) {
            data.prefix_url = window.options.prefix_url;
        }
        if (normalSearch !== true && window.advance) {
            data.advance = 'true';
        }
        G.go(G.url('index', 'local', data));
    };
});